package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.system.domain.Echarts;
import com.ruoyi.system.domain.StuCertificate;
import com.ruoyi.system.domain.StuDataCerState;
import com.ruoyi.system.mapper.StuDataCerStateMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StuDataCer;
import com.ruoyi.system.service.IStuDataCerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学生信息和证书关联表Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@Controller
@RequestMapping("/system/stuDataCer")
public class StuDataCerController extends BaseController
{
    private String prefix = "system/stuDataCer";

    private Long nowUserId;
    private Long deptId;
    @Autowired
    private StuDataCerStateMapper stuDataCerStateMapper;

    @Autowired
    private IStuDataCerService stuDataCerService;

    @RequiresPermissions("system:stuDataCer:view")
    @GetMapping()
    public String stuDataCer()
    {
        return prefix + "/stuDataCer";
    }

    /**
     * 查询学生信息和证书关联表列表
     */
    @RequiresPermissions("system:stuDataCer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StuDataCer stuDataCer)
    {
        startPage();
        // 获取当前用户ID和账号类型
        nowUserId = (Long) PermissionUtils.getPrincipalProperty("userId");
        deptId = (Long) PermissionUtils.getPrincipalProperty("deptId");
        List<StuDataCer> list = stuDataCerService.selectStuDataCerList(stuDataCer);
        return getDataTable(list);
    }

    /**
     * 导出学生信息和证书关联表列表
     */
    @RequiresPermissions("system:stuDataCer:export")
    @Log(title = "学生信息和证书关联表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StuDataCer stuDataCer)
    {
        List<StuDataCer> list = stuDataCerService.selectStuDataCerList(stuDataCer);
        ExcelUtil<StuDataCer> util = new ExcelUtil<StuDataCer>(StuDataCer.class);
        return util.exportExcel(list, "学生信息和证书关联表数据");
    }

    /**
     * 新增学生信息和证书关联表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生信息和证书关联表
     */
    @RequiresPermissions("system:stuDataCer:add")
    @Log(title = "学生信息和证书关联表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StuDataCer stuDataCer)
    {
        return toAjax(stuDataCerService.insertStuDataCer(stuDataCer));
    }

    /**
     * 修改学生信息和证书关联表
     */
    @RequiresPermissions("system:stuDataCer:edit")
    @GetMapping("/edit/{studataCerId}")
    public String edit(@PathVariable("studataCerId") Long studataCerId, ModelMap mmap)
    {
        StuDataCer stuDataCer = stuDataCerService.selectStuDataCerByStudataCerId(studataCerId);
        mmap.put("stuDataCer", stuDataCer);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生信息和证书关联表
     */
    @RequiresPermissions("system:stuDataCer:edit")
    @Log(title = "学生信息和证书关联表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StuDataCer stuDataCer)
    {
        return toAjax(stuDataCerService.updateStuDataCer(stuDataCer));
    }

    /**
     * 删除学生信息和证书关联表
     */
    @RequiresPermissions("system:stuDataCer:remove")
    @Log(title = "学生信息和证书关联表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stuDataCerService.deleteStuDataCerByStudataCerIds(ids));
    }

    /**
     * 证书下载
     */
    @RequiresPermissions("system:stuDataCer:download")
    @GetMapping("/download/resource")
    public void resourceDownload(String studataCerId, HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        StuDataCer stuDataCer = stuDataCerService.selectStuDataCerByStudataCerId(Convert.toLong(studataCerId));
        // 本地资源路径
        String localPath = RuoYiConfig.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(stuDataCer.getFilePath(), Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, ".");
        downloadName =stuDataCer.getCerName() + "_" + stuDataCer.getStuNumber() + "_" + stuDataCer.getStuName() + "_" +  stuDataCer.getCerId() + "." + downloadName;
        //设置参数
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

    /**
     * 批量下载
     */
    @RequiresPermissions("system:stuDataCer:download")
    @GetMapping("/download/resources")
    public void downloadFiles(String studataCerIds, HttpServletRequest request, HttpServletResponse response){
        stuDataCerService.downloadFiles(studataCerIds, request,  response);
    }

    /**
     * 公开状态修改
     */
    @Log(title = "学生和证书管理", businessType = BusinessType.UPDATE)
    @PostMapping("/changeIspublic")
    @ResponseBody
    public AjaxResult changeIspublic(StuDataCer stuDataCer)
    {
        return toAjax(stuDataCerService.changeIspublic(stuDataCer));
    }

    /**
     * 审核表
     */
    @RequiresPermissions("system:stuDataCer:review")
    @GetMapping("/review/{studataCerId}")
    public String review(@PathVariable("studataCerId") Long studataCerId, ModelMap mmap)
    {
        StuDataCer stuDataCer = stuDataCerService.selectStuDataCerByStudataCerId(studataCerId);
        mmap.put("stuDataCer", stuDataCer);
        return prefix + "/review";
    }

    /**
     * 新增审核详情信息
     */
    @Log(title = "新增审核详情信息", businessType = BusinessType.INSERT)
    @PostMapping("/addReview")
    @ResponseBody
    public AjaxResult addReviewSave(StuDataCerState stuDataCerState)
    {
        stuDataCerState.setReviewTime(DateUtils.getNowDate());
        stuDataCerState.setReviewBy(getLoginName());
        return toAjax(stuDataCerService.insertStuDataCerState(stuDataCerState));
    }

    /**
     * 审核批量通过
     */
    @RequiresPermissions("system:stuDataCer:review")
    @Log(title = "批量通过", businessType = BusinessType.UPDATE)
    @PostMapping( "/reviews")
    @ResponseBody
    public AjaxResult reviews(String studataCerIds)
    {
        return toAjax(stuDataCerService.reviewByStudataCerIds(studataCerIds));
    }

    /**
     * 审核详情表
     */
    @RequiresPermissions("system:stuDataCer:reviewInfo")
    @GetMapping("/reviewInfo/{studataCerId}")
    public String reviewInfo(@PathVariable("studataCerId") Long studataCerId, ModelMap mmap)
    {
        StuDataCer stuDataCer = stuDataCerService.selectStuDataCerByStudataCerId(studataCerId);
        mmap.put("stuDataCer", stuDataCer);
        return prefix + "/reviewInfo";
    }

    /** 审核详情列表*/
    @PostMapping("/reviewInfo/list/{studataCerId}")
    @ResponseBody
    public TableDataInfo reviewInfoList(@PathVariable("studataCerId") Long studataCerId,StuDataCerState stuDataCerState)
    {
        startPage();
        stuDataCerState.setDataCerStateId(studataCerId);
        List<StuDataCerState> list = stuDataCerStateMapper.selectStuDataCerStateList(stuDataCerState);
        return getDataTable(list);
    }

    @GetMapping("/echartsReviewState")
    public String Echarts()
    {
        return prefix + "/echartsReviewState";
    }
    @PostMapping("/echartsReviewState/{deptId}")
    @ResponseBody
    public Object pieEcharts(@PathVariable("deptId") Long deptId)throws Exception
    {
        StuDataCer stuDataCer = new StuDataCer();
        stuDataCer.setDeptId(deptId);
        HashMap<String, Object> map = new HashMap<>();
        ArrayList<Echarts> varList = stuDataCerService.reviewStataCount(stuDataCer);
        ArrayList<Object> list = new ArrayList<>();
        ArrayList<Object> arrayList = new ArrayList<>();
        for (Echarts echarts : varList) {
            HashMap<Object, Object> hashMap = new HashMap<>();
            String name = echarts.getName();
            list.add(name);
            int value = echarts.getValue();
            hashMap.put("name",name);
            hashMap.put("value",value);
            arrayList.add(hashMap);
        }
        map.put("list",list);

        map.put("varList",arrayList);
        return map;
    }
}
